package com.starsoft.trade.trader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.trade.bean.TradeAcount;

public abstract class AbstractTrader implements Trader{
	
	private static Logger logger = LoggerFactory.getLogger(AbstractTrader.class);
	
	protected TraderContextService contextService;

	protected CloseableHttpClient httpClient;
	
	protected TradeAcount acount;
	
	protected Map<String, String> config;
	
	protected Map<String, String> apiMap;
	
	@SuppressWarnings("unchecked")
	@Override
	public void init(TradeAcount acount, Map<String, String> apiMap, TraderContextService contextService){
		this.acount = acount;
		this.apiMap = apiMap;
		this.contextService = contextService;
		
		try {
			String configStr = acount.getExSystemConfig();
			ObjectMapper om = new ObjectMapper();
			Map<String, String> configMap = new HashMap<>();
			if(!StringUtil.isEmpty(configStr) && configStr.length()>2){
				configMap = om.readValue(configStr, Map.class);
			}
			this.config = configMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpClient = HttpClients.createDefault();
	}
	
	public String httpPost(String url, Header[] headers, List<NameValuePair> params) {
		try {
			logger.info("Trader post:"+url);
			HttpPost post = new HttpPost(url);
			if (headers != null) {
				post.setHeaders(headers);
			}
			if (params != null && !params.isEmpty()) {
				post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
			}
			HttpResponse httpresponse = httpClient.execute(post);
			String body = EntityUtils.toString(httpresponse.getEntity());
			return body;
		} catch (Exception e) {
			throw new RuntimeException(url, e);
		}
	}
	
	public String httpGet(String url, Header[] headers) {
		try {
			logger.info("Trader get:"+url);
			HttpGet get = new HttpGet(url);
			if (headers != null) {
				get.setHeaders(headers);
			}
			HttpResponse httpresponse = httpClient.execute(get);
			String body = EntityUtils.toString(httpresponse.getEntity());
			return body;
		} catch (Exception e) {
			throw new RuntimeException(url, e);
		}
	}
	
	public JsonNode getJsonNode(String str) {
		try {
			ObjectMapper obMapper = new ObjectMapper();
			JsonNode root = obMapper.readTree(str);
			return root;
		} catch (Exception e) {
			throw new RuntimeException("解析json失败", e);
		}
	}
}
